/* ============================================================================ */
/*                  IMPORT DATA AND DEFINE GLOBAL VARIABLES                     */
/* ============================================================================ */

clear all // Clear stored data and variables

/* ============================================================================ */
/*                      LOAD SHIFT-SHARE DATASET (FIRST DATASET)                */
/* ============================================================================ */

/* Import data from CSV */
import delimited "$input\shift_share_data_set.csv", clear

/* Convert date variables */
gen time = date(date,"YMD")
gen qdate = qofd(time)  // Convert to quarterly date format
format time %td
format qdate %tq

gen startoutsideloan = date(datestart,"YMD")
gen qstartoutsideloan = qofd(startoutsideloan)
format startoutsideloan %td
format qstartoutsideloan %tq

/* Create time-related indicators */
sort siren qdate
gen p2pqtrcapturing = qdate >= qstartoutsideloan if p2p == 1
gen outsideloanqtrcapturing = qdate >= qstartoutsideloan if p2p == 0
gen p2pqtrcapturingstrict = qdate > qstartoutsideloan if p2p == 1
gen outsideloanqtrcapturings = qdate > qstartoutsideloan if p2p == 0

/* Compute time difference in quarters */
gen deltaquarter = qdate - qstartoutsideloan

/* Create a dummy for relevant time periods */
gen deltaqdummy = deltaquarter >= -4 & deltaquarter <= 8
replace deltaqdummy = . if deltaquarter == .

/* ============================================================================ */
/*                      INDUSTRY CLASSIFICATION AND CLEANING                    */

/* Pool public sector industries */
replace industry = "public" if inlist(industry, "P", "Q", "R", "S")

/* Merge D and E industries */
replace industry = "DE" if inlist(industry, "D", "E")

/* Merge F and L industries */
replace industry = "FL" if inlist(industry, "F", "L")

/* Remove specific industries */
drop if industry == "A"  // Agriculture
drop if industry == "K"  // Finance & Insurance
drop if industry == "O"  // Public Administration
drop if industry == "B"  // Industry B
drop if industry == "DE" // Not enough firms in DE
drop if industry == "0000Z" // Industry with little information

/* Generate industry groups */
egen industry10 = group(industry)

/* ============================================================================ */
/*                           RATING CLEANING                                    */
/* ============================================================================ */

/* Standardize rating categories */
replace rating = "3A" if rating == "3++"
replace rating = "3B" if rating == "3+"
replace rating = "3C" if rating == "3"
replace rating = "4A" if rating == "4+"
replace rating = "4B" if rating == "4"
replace rating = "5A" if rating == "5+"
replace rating = "5B" if rating == "5"

/* Convert ratings to numeric */
encode rating, gen(ratinge)
replace ratinge = ratinge - 1
replace ratinge = 13 if ratinge == 0


/* ============================================================================ */
/*                     SET PANEL DATA STRUCTURE                                 */
/* ============================================================================ */

tsset siren qdate, quarterly  // Set time-series panel

/* Keep only observations within the relevant time period */
keep if qdate < qofd(date("20200101","YMD"))
keep if qdate > qofd(date("20131231","YMD"))

/* ============================================================================ */
/*                SAVE TEMPORARY DATASET (SHIFT-SHARE DATA)                     */
/* ============================================================================ */

capture log close
log using "$logs\shift_share_search_combination.log", replace

tempfile dataset1
save `dataset1'

/* ============================================================================ */
/*                LOAD MAIN DATASET BEFORE PROPENSITY SCORE MATCHING            */
/* ============================================================================ */

use "$output\ifp_scrapped_aggregated_industries_modified.dta", clear

/* Keep only quarterly data */
keep if inlist(datemonth, 3, 6, 9, 12)
format mdate %tm
gen qdate = qofd(dofm(mdate))
format qdate %tq

/* Compute financial variables */
gen lloan = log(ltloan + bm + bi)
gen lttotal = ltloan + bm + bi
gen lloanfilled = lloan
gen lloan_total = log(stloan + ltloan + bm + bi + oc)
sort siren qdate

/* ============================================================================ */
/*                  KEEP ONLY FIRMS IN T-TEST SAMPLE                            */
/* ============================================================================ */

merge m:1 siren using "$output\ifp_ttest_siren_list.dta", keepusing(siren)
keep if _merge == 3
drop _merge

/* ============================================================================ */
/*                   CREATE CREDIT LINE INDICATOR VARIABLE                      */
/* ============================================================================ */

preserve
keep if delta < 0
collapse (sum) co, by(siren)
gen codummy = co > 0
drop co
save "$output\data_co_dummy.dta", replace
restore

/* ============================================================================ */
/*                           CLEAN & MERGE DATA                                 */
/* ============================================================================ */

egen ratingint = group(rating)
replace ratingint = ratingint - 1
replace ratingint = 13 if ratingint == 0

merge m:1 siren using "$output\data_co_dummy.dta"

/* ============================================================================ */
/*                  DEFINE VARIABLE LABELS AND GLOBAL CONTROLS                  */
/* ============================================================================ */

label var r1filled          "Working capital"
label var r3filled          "EBIT"
label var r5filled          "Sales"
label var r12filled         "Investment"
label var r13filled         "Debt"
label var logassets_filled  "Total Assets"
label var logemployees      "Employment"
label var r_collateral_filled "Tangible assets"
label var rdcostsfilled     "R&D costs filled"
label var ratingint         "Rating"
label var codummy           "$\mathbbm{1}$(Credit line)"

global control `"r1filled r3filled r5filled r12filled r13filled logassets_filled logemployees r_collateral_filled ratingint"'

/* Keep only relevant variables */
keep qdate siren lloan lloan_total $control outsideloan loantype lttotal

/* Merge the datasets */
merge 1:1 siren qdate using `dataset1', nogen 

/* Keep only data from 2014Q1 onwards */
keep if qdate >= tq(2014q1)

tempfile dataset2
save `dataset2'

/* ============================================================================ */
/*                 ADD PROPENSITY SCORE MATCHING (PSM) WEIGHTS                   */
/* ============================================================================ */

use "$output\matched_sample_nn.dta", clear

keep if mdate < mofd(date("20200101","YMD"))
keep if mdate > mofd(date("20131231","YMD"))

/* Keep quarterly data only */
keep if inlist(datemonth, 3, 6, 9, 12)

format mdate %tm
gen qdate = qofd(dofm(mdate))
format qdate %tq
sort siren qdate

/* Add fixed effect labels */
estadd local yearfirm "Y", replace   
estadd local mdateFE "Y", replace  
keep qdate siren weight year

merge 1:1 siren qdate using `dataset2', nogen 

/* ============================================================================ */
/*                     CREATE ADDITIONAL VARIABLES & CONTROLS                   */
/* ============================================================================ */


egen ind_qtr = group(industry10 qdate)
egen size_qtr = group(size qdate)
egen reg_qtr = group(region qdate)
egen rat_qtr = group(ratinge qdate)

replace outsideloanqtrcapturing = 0 if outsideloanqtrcapturing == .
replace p2pqtrcapturing = 0 if p2pqtrcapturing == .
gen post = p2pqtrcapturing + outsideloanqtrcapturing

replace outsideloanqtrcapturings = 0 if outsideloanqtrcapturings == .
replace p2pqtrcapturingstrict = 0 if p2pqtrcapturingstrict == .
gen poststrict = p2pqtrcapturingstrict + outsideloanqtrcapturings

/* Create dummy control variables */
foreach var in $control {
    gen `var'_0 = `var'
    replace `var'_0 = 0 if `var' == .
    gen dum_`var' = (`var' == .)
}

global control_0 `"*_0 dum_*"'

	
/* ============================================================================ */
/*                     MAIN TABLE - STATISTICAL DESCRIPTION                     */
/* ============================================================================ */

	* Set panel structure and sort data
	tsset siren qdate, quarterly
	sort siren qdate
	
	* Define loan security score based on count of secured loans
	gen scoredeltap2p = secured02
	
	* Keep score only in the quarter when the P2P loan was granted
	replace scoredeltap2p = . if p2pqtrcapturing == 0
	replace scoredeltap2p = . if !(L.p2pqtrcapturing == 0 & p2pqtrcapturing == 1)
	by siren: carryforward scoredeltap2p, replace
	by siren: replace scoredeltap2p = scoredeltap2p[_N]
	
	* Define the loan security score for bank borrowers
	gen scoredeltacontran = secured02
	replace scoredeltacontran = . if outsideloanqtrcapturing == 0
	replace scoredeltacontran = . if !(L.outsideloanqtrcapturing == 0 & outsideloanqtrcapturing == 1)
	by siren: carryforward scoredeltacontran, replace
	by siren: replace scoredeltacontran = scoredeltacontran[_N]
	
	* Assign secured scores to the corresponding borrower type
	gen scoredeltaboth = scoredeltacontran if p2p == 0
	replace scoredeltaboth = scoredeltap2p if p2p == 1
	gen scoredeltapost = scoredeltaboth * post
	
	/* ============================================================================ */
	/*                     CREDIT SUPPLY SHIFT-SHARE CONTROL                        */
	/* ============================================================================ */

	* Define credit supply volume variable
	gen volloanp2pv = logloan02
	
	* Keep the value fixed by SIREN and assign at the P2P loan granting quarter
	replace volloanp2pv = . if p2pqtrcapturing == 0
	replace volloanp2pv = . if !(L.p2pqtrcapturing == 0 & p2pqtrcapturing == 1)
	by siren: carryforward volloanp2pv, replace
	by siren: replace volloanp2pv = volloanp2pv[_N]
	
	* Define credit supply variable for bank borrowers
	gen volloancontranv = logloan02
	replace volloancontranv = . if outsideloanqtrcapturing == 0
	replace volloancontranv = . if !(L.outsideloanqtrcapturing == 0 & outsideloanqtrcapturing == 1)
	by siren: carryforward volloancontranv, replace
	by siren: replace volloancontranv = volloancontranv[_N]
	
	* Assign credit supply control variable to corresponding borrower type
	gen volloanbothv = volloancontranv if p2p == 0
	replace volloanbothv = volloanp2pv if p2p == 1
	gen volloanv = volloanbothv * post

/* ============================================================================ */
/*                      MAIN REGRESSIONS (OLS AND IV)                           */
/* ============================================================================ */
	
	*OLS
	eststo olsfull	: reghdfe lloan p2pqtrcapturing post $control_0 if  deltaqdummy, cl(siren) a(qdate siren)
	estadd local qdate    	"Y", replace
	estadd local siren		"Y", replace
	estadd local control	"Y", replace
	qui levelsof siren if p2p == 1 & e(sample), local(sirens_p2p1)
	qui levelsof siren if p2p == 0 & e(sample), local(sirens_p2p0)
	local num_sirens_p2p1: word count `sirens_p2p1'
	local num_sirens_p2p0: word count `sirens_p2p0'
	estadd local num_sirens_p2p1 `num_sirens_p2p1', replace
	estadd local num_sirens_p2p0 `num_sirens_p2p0', replace
	
	*OLS - restricted sample
	eststo ols	: reghdfe lloan p2pqtrcapturing post volloanv $control_0 if deltaqdummy, cl(siren) a(qdate siren)
	estadd local qdate    	"Y", replace
	estadd local siren		"Y", replace
	estadd local control	"Y", replace
	qui levelsof siren if p2p == 1 & e(sample), local(sirens_p2p1)
	qui levelsof siren if p2p == 0 & e(sample), local(sirens_p2p0)
	local num_sirens_p2p1: word count `sirens_p2p1'
	local num_sirens_p2p0: word count `sirens_p2p0'
	estadd local num_sirens_p2p1 `num_sirens_p2p1', replace
	estadd local num_sirens_p2p0 `num_sirens_p2p0', replace
	
	*Second stage (IV estimate)
	eststo second: ivreghdfe lloan (p2pqtrcapturing=scoredeltapost) post volloanv $control_0  if deltaqdummy, cl(siren) a(qdate siren)
	estadd local qdate    	"Y", replace
	estadd local siren		"Y", replace
	estadd local control	"Y", replace
	qui levelsof siren if p2p == 1 & e(sample), local(sirens_p2p1)
	qui levelsof siren if p2p == 0 & e(sample), local(sirens_p2p0)
	local num_sirens_p2p1: word count `sirens_p2p1'
	local num_sirens_p2p0: word count `sirens_p2p0'
	estadd local num_sirens_p2p1 `num_sirens_p2p1', replace
	estadd local num_sirens_p2p0 `num_sirens_p2p0', replace
	gen sample = e(sample)
	estadd scalar f2 = e(widstat)
	
	*First stage
	eststo first1: reghdfe p2pqtrcapturing scoredeltapost post volloanv $control_0 if sample, cl(siren) a(qdate siren) 
	estadd local qdate    	"Y", replace
	estadd local siren		"Y", replace
	estadd local control	"Y", replace
	qui levelsof siren if p2p == 1 & e(sample), local(sirens_p2p1)
	qui levelsof siren if p2p == 0 & e(sample), local(sirens_p2p0)
	local num_sirens_p2p1: word count `sirens_p2p1'
	local num_sirens_p2p0: word count `sirens_p2p0'
	estadd local num_sirens_p2p1 `num_sirens_p2p1', replace
	estadd local num_sirens_p2p0 `num_sirens_p2p0', replace
		
	*Reduced form
	eststo rf1	: reghdfe lloan scoredeltapost post volloanv $control_0 if sample, cl(siren) a(qdate siren)	
	estadd local qdate    	"Y", replace
	estadd local siren		"Y", replace
	estadd local control	"Y", replace
	qui levelsof siren if p2p == 1 & e(sample), local(sirens_p2p1)
	qui levelsof siren if p2p == 0 & e(sample), local(sirens_p2p0)
	local num_sirens_p2p1: word count `sirens_p2p1'
	local num_sirens_p2p0: word count `sirens_p2p0'
	estadd local num_sirens_p2p1 `num_sirens_p2p1', replace
	estadd local num_sirens_p2p0 `num_sirens_p2p0', replace
	
/* ============================================================================ */
/*                      TABLE 8a                                                */
/* ============================================================================ */

	label var scoredeltapost "Secured ratio $\times$ Post"
	label var post "Post"
	label var p2pqtrcapturing "FinTech $\times$ Post"
	label var volloanv "Credit supply  $\times$ Post"

	esttab olsfull ols first1 rf1 second using "$tables\shift-share.tex", replace ///
		prehead(" &\multicolumn{2}{c}{log(Long-term credit)}  &\multicolumn{1}{c}{FinTech}&\multicolumn{3}{c}{log(Long-term credit)} \\    \cmidrule(lr){2-3} \cmidrule(lr){4-4} \cmidrule(lr){4-7}") ///
		posthead("&\multicolumn{2}{c}{OLS} &\multicolumn{1}{c}{First stage} &\multicolumn{1}{c}{Reduced form} &\multicolumn{1}{c}{IV}\\ \midrule ") ///
		star(* 0.10 ** 0.05 *** 0.01) nomtitles fragment gap booktabs label nonote noobs eqlabels(,none) b(%9.3f) se(%9.3f) ///
		stats(qdate siren control N f2 r2, labels("Quarter FE" "Firm FE" "Controls" "\midrule Observations" "F-stat" "R-sq") ///
		layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") ///
		fmt(%9.0fc %9.0fc %9.0fc %9.0fc  %9.3fc %9.3fc)) ///
		keep(scoredeltapost post p2pqtrcapturing volloanv) ///
		order(scoredeltapost p2pqtrcapturing post volloanv) ///
		mgroups("FinTech", pattern(1 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))	

/* ============================================================================ */
/*                     WEAK INSTRUMENT TESTS                                    */
/* ============================================================================ */

* Anderson-Rubin weak IV test
keep if deltaqdummy
xtdata lloan p2pqtrcapturing scoredeltapost post volloanv qdate_dummy* $control_0, i(siren) fe clear
ivreg2 lloan post volloanv qdate_dummy* $control_0 (p2pqtrcapturing = scoredeltapost), cl(siren)
weakiv

* Lee et al. weak IV test
/*
net install tf, force from(http://www.princeton.edu/~davidlee/wp/)
help tf
*/

tf lloan post volloanv qdate_dummy* $control_0 (p2pqtrcapturing = scoredeltapost)
